package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constrant.MessageConstant;
import com.itheima.constrant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * @author FCJ
 * @date 2020-01-03 19:04
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService setmealService;
    @Reference
    private CheckGroupService checkGroupService;


    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.addrelation(setmeal, checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.update(setmeal, checkgroupIds);
            return new Result(true, "更新套餐成功");
        } catch (Exception e) {
            return new Result(false, "更新套餐失败");
        }
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {

        String originalFilename = imgFile.getOriginalFilename();//获取原始文件名
        int index = originalFilename.lastIndexOf(".");//获取点的位置
        String substring = originalFilename.substring(index - 1);//获取文件后缀
        String fileName = UUID.randomUUID().toString() + substring;//生成随机文件名
        try {//将文件上传到七牛云
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }

    @RequestMapping("/findSetmealCount")
    public Result findSetmealCount() {
        try {
            Map<String, Object> map = setmealService.findSetmealCount();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);

        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setmealService.findSetmealById(id);
            List<CheckGroup> AllCheckGroups = checkGroupService.findAllCheckGroup();
            List<Integer> checkgroupIds = setmealService.findGroupIdBySetmealId(id);
            Map map = new HashMap<String, Object>();
            map.put("setmeal", setmeal);
            map.put("checkGroups", AllCheckGroups);
            map.put("checkgroupIds", checkgroupIds);
            return new Result(true, "查询套餐信息成功", map);
        } catch (Exception e) {
            return new Result(false, "查询套餐信息失败");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Setmeal> setmeals = setmealService.findAll();
            return new Result(true, "查询套餐信息成功", setmeals);
        } catch (Exception e) {
            return new Result(false, "查询套餐信息失败");
        }
    }
}
